package com.itheima.health.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.health.resource.entity.QueryPageBean;
import com.itheima.health.service.CheckGroupSrvice;
import com.itheima.mapper.CheckGroupMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Transactional
public class CheckGroupSrviceImpl extends ServiceImpl<CheckGroupMapper, CheckGroup> implements CheckGroupSrvice {

    @Override
    public void add(CheckGroup checkGroup, Integer[] checkitemIds) {
        baseMapper.insert(checkGroup);
        //获取CheckGroup中的id
        int groupId = checkGroup.getId();
        //把id传入中间表的groupId中
        if(checkitemIds.length!=0 && checkitemIds!=null){
            for (Integer checkitemId : checkitemIds) {
                //添加CheckItemId和CheckGroupId到中间表去
                baseMapper.addCheckItemIdAndCheckGroupId(groupId,checkitemId);
            }
        }
    }

    @Override
    public Map findPageByCondition(QueryPageBean queryPageBean) {
        // LambdaQueryWrapper<CheckGroup> lambda = new QueryWrapper<CheckGroup>().lambda();
        // String queryString = queryPageBean.getQueryString();
        // queryString=queryString==null?"":queryString;
        // //对象名：：方法名
        // lambda.eq(CheckGroup::getIs_delete,0);//默认and
        // lambda.like(CheckGroup::getName,queryString).or().like(CheckGroup::getHelpCode,queryString);
        //  select  * from xxx where name like  xx  and  is_delete = 0

        QueryWrapper<CheckGroup> queryWrapper = new QueryWrapper<CheckGroup>();
        String queryString = queryPageBean.getQueryString();//条件
        queryWrapper.like("name",queryString==null?"":queryString);
        queryWrapper.eq("is_delete",0);//  有效数据

        //分页请求参数条件获取page对象
        Page<CheckGroup> page = page(new Page<CheckGroup>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize()),queryWrapper);
        //获取每页请求的数据封装到list集合中给实体类
        List<CheckGroup> checkGroupList = page.getRecords();
        //获取总记录数将其封装到map集合中
        long total = page.getTotal();
        HashMap map = new HashMap();
        map.put("list",checkGroupList);
        map.put("total",total);
        return map;
    }


    @Override
    public List<CheckGroup> findAll() {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.eq("is_delete",0);//  有效数据
        return list(queryWrapper);
    }
}
